﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.Entity;
using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Util;
using NHibernate;
using ComponentFactory.Krypton.Toolkit;

namespace BotonJXC.Forms.Controls
{
    public partial class WarehouseControl : BaseInfoControl
    {
        string _warehouseName;
        short _warehouseType;
        short _warehouseLevel;
        WarehouseBLL _warehouseBll = new WarehouseBLL();
        StoreBLL _storeBll = new StoreBLL();
        DepartmentBLL _departmentBll = new DepartmentBLL();
        IList<Department> departments = null;
        IList<Store> stores = null;
        public WarehouseControl()
        {
            InitializeComponent();
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _warehouseName = ktbWarehouseName.Text.Trim();
            _warehouseType = (short)(kcbWarehouseType.SelectedIndex + 1);
            _warehouseLevel = (short)(kcbWarehouseLevel.SelectedIndex + 1);
            LoadData();
        }

        private void WarehouseControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                IList<ComboBoxDropItem> items = OrgLevelUtil.CreateOrgLevelList();
                kcbWarehouseLevel.DataSource = items;
                foreach (ComboBoxDropItem item in items)
                {
                    this.kdgvcbcWarehouseLevel.Items.Add(item.Text);
                }
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<Warehouse> pageModel = _warehouseBll.FindByPage(_warehouseName, _warehouseLevel, _warehouseType, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            kdgvWarehouse.DataSource = new BindingList<Warehouse>(pageModel.Results);

            departments = _departmentBll.FindAll();
            stores = _storeBll.FindAll();
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvWarehouse.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvWarehouse.Rows)
                {
                    Warehouse warehouse = r.DataBoundItem as Warehouse;
                    if (warehouse == null)
                        continue;
                    if (warehouse.WarehouseId == 0)
                    {
                        _warehouseBll.Save(warehouse);
                    }
                    else if ("1".Equals(r.Tag) && warehouse.WarehouseId != 0)
                    {
                        _warehouseBll.Update(warehouse);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void AddInfo()
        {

        }

        public override void DeleteInfo()
        {
            IList<Warehouse> _warehouseDatas = (IList<Warehouse>)kdgvWarehouse.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvWarehouse.SelectedRows)
                {
                    Warehouse warehouse = r.DataBoundItem as Warehouse;
                    if (warehouse != null)
                    {
                        _warehouseBll.Delete(warehouse.WarehouseId);
                    }
                    _warehouseDatas.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvWarehouse.Refresh();
        }

        private void kdgvWarehouse_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            IList<Warehouse> _warehouseDatas = (IList<Warehouse>)kdgvWarehouse.DataSource;
            if (_warehouseDatas != null && _warehouseDatas.Count > e.RowIndex)
            {
                string colName = kdgvWarehouse.Columns[e.ColumnIndex].Name;
                if (colName == "kdgvcbcWarehouseType")//类型
                {
                    switch (_warehouseDatas[e.RowIndex].WarehouseType)
                    {
                        case 1:
                            e.Value = "门店";
                            break;
                        case 2:
                            e.Value = "部门";
                            break;
                        case 3:
                            e.Value = "人员";
                            break;
                    }
                }
                else if (colName == "kdgvcbcWarehouseLevel") //级别
                {
                    e.Value = _warehouseDatas[e.RowIndex].WarehouseLevel + "级别";
                }
                else if (colName == "KCTypeObject")
                {
                    if (_warehouseDatas[e.RowIndex].WarehouseType == 1)
                    {
                        Store s = FindStore(_warehouseDatas[e.RowIndex].AttriToId);
                        if (s != null)
                            e.Value = s.StoreName;
                    }
                    else if (_warehouseDatas[e.RowIndex].WarehouseType == 2)
                    {
                        Department d = FindDepartment(_warehouseDatas[e.RowIndex].AttriToId);
                        if (d != null)
                            e.Value = d.DepName;
                    }
                }
            }
        }

        Store FindStore(int storeId)
        {
            foreach (Store s in stores)
            {
                if (s.StoreId == storeId)
                    return s;
            }
            return null;
        }

        Department FindDepartment(int depId)
        {
            foreach (Department d in departments)
            {
                if (d.DepId == depId)
                    return d;
            }
            return null;
        }

        private void kdgvWarehouse_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string colName = kdgvWarehouse.Columns[kdgvWarehouse.CurrentCell.ColumnIndex].Name;
            IList<Warehouse> storeDatas = (IList<Warehouse>)kdgvWarehouse.DataSource;
            KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
            if (colName == "kdgvcbcWarehouseType" || colName == "kdgvcbcWarehouseLevel" || colName == "KCTypeObject")
            {
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
            if (colName == "KCTypeObject")
            {
                KCTypeObject.Items.Clear();
                if (storeDatas[kdgvWarehouse.CurrentCell.RowIndex].WarehouseType == 1)
                {
                    foreach (Store s in stores)
                    {
                        KCTypeObject.Items.Add(s.StoreName);
                    }
                }
                else if (storeDatas[kdgvWarehouse.CurrentCell.RowIndex].WarehouseType == 2)
                {
                    foreach (Department d in departments)
                    {
                        KCTypeObject.Items.Add(d.DepName);
                    }
                }
            }
        }

        /// <summary>
        /// 类型选择后事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            IList<Warehouse> storeDatas = (IList<Warehouse>)kdgvWarehouse.DataSource;
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            string colName = kdgvWarehouse.Columns[kdgvWarehouse.CurrentCell.ColumnIndex].Name;
            
            if (colName == "kdgvcbcWarehouseType")
            {
                storeDatas[kdgvWarehouse.CurrentCell.RowIndex].WarehouseType = (short)(cbc.SelectedIndex + 1);
            }
            else if (colName == "kdgvcbcWarehouseLevel")
            {
                storeDatas[kdgvWarehouse.CurrentCell.RowIndex].WarehouseLevel = (short)(cbc.SelectedIndex + 1);
            }
            else if (colName == "KCTypeObject" && storeDatas[kdgvWarehouse.CurrentCell.RowIndex].WarehouseType == 1)
            {
                storeDatas[kdgvWarehouse.CurrentCell.RowIndex].AttriToId = cbc.SelectedIndex>-1 ? stores[cbc.SelectedIndex].StoreId : 0;
            }
            else if (colName == "KCTypeObject" && storeDatas[kdgvWarehouse.CurrentCell.RowIndex].WarehouseType == 2)
            {
                storeDatas[kdgvWarehouse.CurrentCell.RowIndex].AttriToId = cbc.SelectedIndex>-1 ? departments[cbc.SelectedIndex].DepId : 0;
            }
        }

        private void kdgvWarehouse_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvWarehouse.Rows[e.RowIndex].Tag = "1";
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}